package com.meixiang.adapter.account;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.moments.ContentEditingActivity;
import com.meixiang.activity.moments.ReplyDetailsActivity;
import com.meixiang.dialog.CustomPopup;
import com.meixiang.entity.moments.PostDetailsList;
import com.meixiang.entity.moments.PostDetailsItemBean;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.recyclerview.RecyclerAdapter;
import com.meixiang.tool.Tool;
import com.meixiang.util.DensityUtil;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.flowlayout.CustomFlowAdapter;
import com.meixiang.view.flowlayout.CustomFlowLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/6/21 15:09
 */
public class MyPostDetailAdapter extends RecyclerAdapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;

    private boolean isAddHead;

    private boolean isAddFoot;
    private int footViewSize = 1, headViewSize = 1;
    private PostDetailsList mBean;
    private List<PostDetailsItemBean> mItemBean= new ArrayList<>();
    private Context context;
    //    private EditPopupWindow mEditPopupWindow;
    private CustomPopup popupWindow ;
    private Activity mActivity ;
    private WindowManager.LayoutParams lp ;

    private TextView mTvEditInvitation,mTvDeleteInvitation;
    public void addAll(List<PostDetailsItemBean> mItemBean){
        this.mItemBean.addAll(mItemBean);
        this.notifyDataSetChanged();
    }
    public void clear(){
        this.mItemBean.clear();
        this.notifyDataSetChanged();
    }
    public void addData(PostDetailsList mBean){
        this.mBean=mBean;

    }

    public MyPostDetailAdapter(Context context, Activity mActivity) {
        this.context = context;
        this.mActivity = mActivity ;
    }


    public int getContentItemCount(){

        return mItemBean.size();
    }
    @SuppressWarnings("unchecked")
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_HEADER) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_my_postdetails , parent, false);
            HeaderViewHolder viewHolder = new HeaderViewHolder(itemView);
            return viewHolder;
        }if(viewType == TYPE_ITEM){
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_reply, parent, false);
            ItemViewHolder viewHolder = new ItemViewHolder(itemView);
            return viewHolder;
        }if (viewType ==TYPE_FOOT){
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.foot_item, parent, false);
            FootViewHolder viewHolder = new FootViewHolder(itemView);
            return viewHolder;
        }
        return null;

    }
    @SuppressWarnings("unchecked")
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            if (mOnItemClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(holder.itemView, pos);

                    }
                });
            }
            if(mItemBean!=null) {
                PostDetailsItemBean mData = mItemBean.get(position-1);
                GlideHelper.showCircleImage(context,mData.getMemberAvatar(),R.drawable.image_default_gray_bg,((ItemViewHolder) holder).mIvUserHeadPortrait);
                ((ItemViewHolder) holder).mTvUserName.setText(mData.getMemberName());
                ((ItemViewHolder) holder).mTvTime.setText(mData.getCommentTime());
                ((ItemViewHolder) holder).mTvContent.setText(mData.getComment());
                if (mData.getAnswerName()==null){
                    ((ItemViewHolder) holder).mTvOtherUserName.setVisibility(View.GONE);
                    ((ItemViewHolder) holder).mTvUserReply.setVisibility(View.GONE);
                }else {

                    ((ItemViewHolder) holder).mTvOtherUserName.setText(mData.getAnswerName());
                }


            }
        } if (holder instanceof HeaderViewHolder) {
            //设置数据
            if(mBean!=null){
                //设置用户图像
                GlideHelper.showCircleImage(context,mBean.getMemberAvatar(),R.mipmap.default_head_portrait,((HeaderViewHolder) holder).mIvUserPortrait);
                ((HeaderViewHolder) holder).mTvUserName.setText(mBean.getMemberName());
                ((HeaderViewHolder) holder).mTvContent.setText(mBean.getComment());
//                ((HeaderViewHolder) holder).mGridImageView.setIsShowAll(mBean.getCommentImages().get(position).isEmpty());
                ((HeaderViewHolder) holder).mCustomFlowLayout.setMargin(0, 5, 5, 0);
                final String[] urls = mBean.getCommentImages();
                if (urls == null || urls.length == 0) {
                    ((HeaderViewHolder) holder).mCustomFlowLayout.setVisibility(View.GONE);
                } else {
                    ((HeaderViewHolder) holder).mCustomFlowLayout.setVisibility(View.VISIBLE);
                    ((HeaderViewHolder) holder).mCustomFlowLayout.setAdapter(new CustomFlowAdapter(urls){
                        @Override
                        public View getView(CustomFlowLayout parent, int position, Object o) {
                            View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_attention_image, parent, false);
                            ImageView imageView = (ImageView) layout.findViewById(R.id.iv_image);
                            GlideHelper.showImage(context, (String) o, R.mipmap.recommend3, imageView);
                            return layout;
                        }
                    });

                }
                ((HeaderViewHolder) holder).mTvPostTime.setText(mBean.getAppearTime());
            }

            ((HeaderViewHolder) holder).mIvAttention.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    initPopupWindow();
                    int xoffInPixels = Tool.getWindowWith(context) / 5 -((HeaderViewHolder) holder).mIvAttention.getWidth();
                    // 将pixels转为dip
                    int xoffInDip = DensityUtil.px2dip(context,xoffInPixels);
                    if (popupWindow.isShowing()) {
                            popupWindow.dismiss();
                    } else {
                        popupWindow.showAsDropDown(((HeaderViewHolder) holder).mIvAttention,-xoffInDip,0);
                        popupWindow.update();
                }
                }
            });
        }
        if (holder instanceof FootViewHolder) {
            ((FootViewHolder) holder).mTvCheckMoreComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    context.startActivity(new Intent(context,ReplyDetailsActivity.class));

                    Intent intent=new Intent(context,ReplyDetailsActivity.class);
                    intent.putExtra("","");
                }
            });
        }

    }

    /**
     * 初始化一个弹出的窗体
     */
    private void initPopupWindow() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_edit_delete_invitation, null);
        mTvEditInvitation= (TextView) view.findViewById(R.id.tv_edit_invitation);
        mTvDeleteInvitation= (TextView) view.findViewById(R.id.tv_delete_invitation);

        popupWindow = new CustomPopup(context,mActivity,view,60,R.mipmap.menu_bg);

        mTvEditInvitation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tool.showTextToast(mActivity,"点击了1");
                /**
                 * 跳转编辑页面
                 * */
                context.startActivity(new Intent(context, ContentEditingActivity.class));
                popupWindow.dismiss();

            }
        });
        mTvDeleteInvitation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                /**
                 * 发送请求
                 * 删除
                 * */

                HttpParams params = new HttpParams();
                params.put("commentId",mBean.getCommentId());

                HttpUtils.post(Config.ACCOUNT_DELETE_MESSAGE, params, new HttpCallBack(mActivity) {
                    @Override
                    public void onSucceed(JSONObject result, String code, String message) {
                        Tool.showTextToast(mActivity,message);
                    }

                    @Override
                    public void onError(String code, String message) {
                        Tool.showTextToast(mActivity,message);
                    }
                });
                popupWindow.dismiss();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mItemBean.size() + headViewSize + footViewSize;
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;
        if (headViewSize==1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize==1 && position == getItemCount()-1) {
            /**
             * 当list的长度大于于3的时候才显示否则不显示底布局
             * */
            type = TYPE_FOOT;
        }
        return type;


    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_user_head_portrait)
        ImageView mIvUserHeadPortrait;
        @Bind(R.id.tv_user_name)
        TextView mTvUserName;
        @Bind(R.id.tv_user_reply)
        TextView mTvUserReply;
        @Bind(R.id.tv_other_user_name)
        TextView mTvOtherUserName;
        @Bind(R.id.tv_time)
        TextView mTvTime;
        @Bind(R.id.tv_date)
        TextView mTvDate;
        @Bind(R.id.tv_content)
        TextView mTvContent;
        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class HeaderViewHolder extends RecyclerView.ViewHolder {
        //用户头像
        @Bind(R.id.iv_user_portrait)
        ImageView mIvUserPortrait;
        //用户姓名
        @Bind(R.id.tv_user_name)
        TextView mTvUserName;
        //帖子内容
        @Bind(R.id.tv_content)
        TextView mTvContent;
        //时间
        @Bind(R.id.tv_post_time)
        TextView mTvPostTime;
        @Bind(R.id.layout_nine_grid)
        CustomFlowLayout mCustomFlowLayout;
        @Bind(R.id.iv_attention)
        ImageView mIvAttention;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_check_more_comment)
        TextView mTvCheckMoreComment;

        public FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
